package DAO;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.*;
import java.util.ArrayList;

public class GoodsDaoImpl implements GoodsDao{
    private  String ip=this.getIp();
    private String url="jdbc:mysql://"+ip+":3306/mygoodssystem?useOldAliasMetadataBehavior=true";
    //获取ip
    public String getIp(){
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("Local HostAddress: "+addr.getHostAddress());
        String hostname = addr.getHostName();
        System.out.println("Local host name: "+hostname);
        return addr.getHostAddress();
    }


    /*
    查询所有商品信息
     */
    @Override
    public ArrayList<Goods> findAll()   {
        ArrayList<Goods> list=new ArrayList<>();
        Connection con=null;
        Statement stat=null;
        ResultSet rs =null;
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
             con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
             stat=con.createStatement();
            //5.执行sql语句，并接收结果
            String sql="SELECT * FROM mygoods ";
             rs = stat.executeQuery(sql);
            //6.处理结果

            while(rs.next()){
                //封装对象
                Goods g=new Goods();
                g.setGoodsNum(rs.getString("goodsNum"));
                g.setGoodsName(rs.getString("goodsName"));
                g.setGoodsPrice(rs.getString("goodsPrice"));
                g.setGoodsListingDate(rs.getString("goodsListingDate"));
                g.setGoodsCategories(rs.getString("goodsCategories"));
                g.setGoodsOrigin(rs.getString("goodsOrigin"));
                list.add(g);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

        return list;
    }

    /*
    条件查询，根据GoodsNum查询商品信息
     */
    @Override
    public  Goods findByGoodsNum(String goodsNum){
        Goods g=new Goods();
        Connection con=null;
        Statement stat=null;
        ResultSet rs =null;
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            String sql="SELECT * FROM mygoods WHERE goodsNum= '"+goodsNum+"'";
            rs = stat.executeQuery(sql);
            //6.处理结果

            while(rs.next()){

                g.setGoodsNum(rs.getString("goodsNum"));
                g.setGoodsName(rs.getString("goodsName"));
                g.setGoodsPrice(rs.getString("goodsPrice"));
                g.setGoodsListingDate(rs.getString("goodsListingDate"));
                g.setGoodsCategories(rs.getString("goodsCategories"));
                g.setGoodsOrigin(rs.getString("goodsOrigin"));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }



        return g;
    }

    /*
    新增商品信息
     */
    @Override
    public  int insert(Goods gds){

        Connection con=null;
        Statement stat=null;
        int result=0;//修改的行数
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
           // SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String sql="INSERT INTO mygoods VALUES ('"+gds.getGoodsNum()+"','"+gds.getGoodsName()+"','"+gds.getGoodsPrice()+"','"+gds.getGoodsListingDate()+"','"+gds.getGoodsCategories()+"','"+gds.getGoodsOrigin()+"')";
           result=stat.executeUpdate(sql);
            //6.处理结果

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //成功弹窗
            new opyes();
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

    /*
    修改商品信息
     */
    @Override
    public  int updata(Goods gds){
        Connection con=null;
        Statement stat=null;
        int result=0;//修改的行数
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            // SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String sql="UPDATE mygoods SET goodsNum='"+gds.getGoodsNum()+"',goodsName='"+gds.getGoodsName()+"',goodsPrice='"+gds.getGoodsPrice()+"',goodsListingDate='"+gds.getGoodsListingDate()+"',goodsCategories='"+gds.getGoodsCategories()+"',goodsOrigin='"+gds.getGoodsOrigin()+"'WHERE goodsNum='"+gds.getGoodsNum()+"'";
            result=stat.executeUpdate(sql);
            //6.处理结果
         //   '"+gds.getGoodsNum()+"','"+gds.getGoodsName()+"','"+gds.getGoodsPrice()+"','"+gds.getGoodsListingDate()+"','"+gds.getGoodsCategories()+"','"+gds.getGoodsOrigin()+"'
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //成功弹窗
            if (result>0)
            new opyes();
            else new opno();
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

    /*
    删除商品信息
     */
    public  int delete(String goodsNum){
        Connection con=null;
        Statement stat=null;
        int result=0;//修改的行数
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            String sql="DELETE FROM mygoods WHERE goodsNum='"+goodsNum+"'";
            result=stat.executeUpdate(sql);
            //6.处理结果
            //   '"+gds.getGoodsNum()+"','"+gds.getGoodsName()+"','"+gds.getGoodsPrice()+"','"+gds.getGoodsListingDate()+"','"+gds.getGoodsCategories()+"','"+gds.getGoodsOrigin()+"'
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //成功弹窗
            if(result>0)
            new opyes();
            else new opno();
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }
    //操作成功弹窗
    class opyes{
        public opyes(){
            // 创建 JFrame 实例
            JFrame frame = new JFrame("系统提示");
            // Setting the width and height of frame
            frame.setSize(350, 200);
            frame.setLocation(500,250);
            frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            frame.setVisible(true);
            JButton j=new JButton("操作成功！！！");
            frame.add(j);
            j.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                }
            });


        }
    }
    //操作失败弹窗
    class opno{
        public opno(){
            // 创建 JFrame 实例
            JFrame frame = new JFrame("系统提示");
            // Setting the width and height of frame
            frame.setSize(350, 200);
            frame.setLocation(500,250);
            frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            frame.setVisible(true);
            JButton j=new JButton("没有该项数据！！！");
            frame.add(j);
            j.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                }
            });}}

}
